define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'electric/index' + location.search,
                    add_url: 'electric/add',
                    edit_url: 'electric/edit',
                    actionsend_url: 'electric/actionsend',
                    del_url: 'electric/del',
                    multi_url: 'electric/multi',
                    import_url: 'electric/import',
                    table: 'electric',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'device.device_name', title: __('Device.device_name'), operate: 'LIKE'},
                        {field: 'strv', title: __('Strv'), operate: 'LIKE'},
                        {field: 'endv', title: __('Endv')},
                        {field: 'ingv', title: __('Ingv')},
                        {field: 'price', title: __('Price')},
                        {field: 'money', title: __('Money')},
                        {field: 'img', title: __('Img'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    var table = $("#table");
    //同步钉钉按钮
    $(document).on("click", ".btn-ding", function () {
        //调起弹窗
        // Fast.api.open("user/user/add_clue?ids="+ids, "分派线索", {
        //     callback: function (value) {
        //         console.log(value);
        //     }
        // });
        //在table外不可以使用添加.btn-change的方法
        //只能自己调用Table.api.multi实现
        //如果操作全部则ids可以置为空
        var that = this;
        Layer.confirm(
            __('确定推送?'),
            {icon: 3, title: __('Warning'), offset: 100, shadeClose: true},
            function (index) {
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                Table.api.multi("changestatus", ids.join(","), table, that);
                Layer.close(index);
            }
        );
    });

    return Controller;
});
